﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace HQHS.Library.Common.MVCExtention
{
    public static class MVCExtension
    {
        public static IHtmlString Paging(this HtmlHelper helper, int pageIndex, int pageCount, string actionName, string controllerName, string q = null)
        {
            StringBuilder sb = new StringBuilder();
            q = string.IsNullOrEmpty(q) ? string.Empty : "?q=" + q;
            if (pageIndex > 0)
            {
                int maxIndex;
                if (pageIndex % 5 == 0)
                {
                    maxIndex = pageIndex;
                }
                else
                {
                    maxIndex = (pageIndex / 5 + 1) * 5;
                }
                int pindex = maxIndex - 4;
                if (maxIndex > pageCount)
                {
                    maxIndex = pageCount;
                }
                if (pageIndex != 1)
                {
                    sb.AppendLine(string.Format("<li><a class=\"prev2 pages\" href=\"/englishfirst/gud/adminconsole/{1}/{0}/1{2}\">&lt;&lt;</a></li>", actionName, controllerName, q));
                    sb.AppendLine(string.Format("<li><a class=\"prev pages\" href=\"/englishfirst/gud/adminconsole/{2}/{0}/{1}{3}\">&lt;</a></li>", actionName, pageIndex - 1, controllerName, q));
                }
                for (int i = pindex; i <= maxIndex; i++)
                {
                    if (i == pageIndex)
                    {
                        sb.AppendLine(string.Format("<li><span class=\"current\">{0}</span></li>", i));
                    }
                    else
                    {
                        sb.AppendLine(string.Format("<li><a class=\"pages\" href=\"/englishfirst/gud/adminconsole/{2}/{0}/{1}{3}\">{1}</a></li>", actionName, i, controllerName, q));
                    }
                }
                if (pageCount > 0 && pageIndex != pageCount)
                {
                    sb.AppendLine(string.Format("<li><a class=\"next pages\" href=\"/englishfirst/gud/adminconsole/{2}/{0}/{1}{3}\">&gt;</a></li>", actionName, pageIndex + 1, controllerName, q));
                    sb.AppendLine(string.Format("<li><a title=\"{1}\" class=\"next2 pages\" href=\"/englishfirst/gud/adminconsole/{2}/{0}/{1}{3}\">&gt;&gt;</a></li>", actionName, pageCount, controllerName, q));
                }
            }

            return MvcHtmlString.Create(sb.ToString());
        }
    }
}
